﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DP.Web.UI;

namespace CRM.Web.Test
{
    public partial class WebForm : Common.PageBase
    {
        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("A", Type.GetType("System.String"));
                dt.Columns.Add("B", Type.GetType("System.String"));
                dt.Columns.Add("C", Type.GetType("System.String"));
                for (int i = 0; i < 10; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["A"] = "A" + i.ToString();
                    dr["B"] = "B" + i.ToString();
                    dr["C"] = "C" + i.ToString();
                    dt.Rows.Add(dr);
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();

            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "A":
                    {
                        TextBox txt = GridView1.FooterRow.FindControl("txtA") as TextBox;
                        TextBox txtb = GridView1.FooterRow.FindControl("txtB") as TextBox;
                        txtb.Text = txt.Text;
                        PageHelper.ShowMessage(txt.Text.Trim());
                    }
                    break;
            }
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        if(e.Row.RowIndex== GridView1.EditIndex)
                        {
                            if ((e.Row.RowIndex % 2) == 0)
                            {
                                if (e.Row.Cells[0].FindControl("A") is TextBox)
                                {
                                    e.Row.Cells[0].Controls.Clear();
                                    DropDownList ddl = new DropDownList();
                                    ddl.ID = "A";
                                    e.Row.Cells[0].Controls.Add(ddl);
                                }
                            }

                        }
                    }
                    break;
            }
        }



        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        if (e.Row.Cells[0].FindControl("A") is DropDownList)
                        {
                            DropDownList ddl = e.Row.Cells[0].FindControl("A") as DropDownList;
                            ddl.Items.Add(new ListItem("A0", "A0"));
                            ddl.Items.Add(new ListItem("A1", "A1"));
                            ddl.Items.Add(new ListItem("A2", "A2"));
                        }
                    }
                    break;
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1; 
            DataTable dt = new DataTable();
            dt.Columns.Add("A", Type.GetType("System.String"));
            dt.Columns.Add("B", Type.GetType("System.String"));
            dt.Columns.Add("C", Type.GetType("System.String"));
            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                dr["A"] = "A" + i.ToString();
                dr["B"] = "B" + i.ToString();
                dr["C"] = "C" + i.ToString();
                dt.Rows.Add(dr);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            DataTable dt = new DataTable();
            dt.Columns.Add("A", Type.GetType("System.String"));
            dt.Columns.Add("B", Type.GetType("System.String"));
            dt.Columns.Add("C", Type.GetType("System.String"));
            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                dr["A"] = "A" + i.ToString();
                dr["B"] = "B" + i.ToString();
                dr["C"] = "C" + i.ToString();
                dt.Rows.Add(dr);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Control c = GridView1.Rows[e.RowIndex].Cells[0].FindControl("A");
            if (c is TextBox)
            {
                TextBox txt = (c as TextBox);
                if (txt != null)
                {
                    lblMessage.Text = txt.Text;
                }

            }
            else if (c is DropDownList)
            {
                DropDownList ddl = c as DropDownList;
                if (ddl != null)
                {
                    lblMessage.Text = ddl.SelectedValue;
                }
            }


            GridView1.EditIndex = -1;
            DataTable dt = new DataTable();
            dt.Columns.Add("A", Type.GetType("System.String"));
            dt.Columns.Add("B", Type.GetType("System.String"));
            dt.Columns.Add("C", Type.GetType("System.String"));
            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                dr["A"] = "A" + i.ToString();
                dr["B"] = "B" + i.ToString();
                dr["C"] = "C" + i.ToString();
                dt.Rows.Add(dr);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}
